package banking8.domain;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-11-08
 * Time:22:50
 */
public class CheckingAccount extends Account {
    double overdraftProtection;//透支保障

    public double getOverdraftProtection() {
        return overdraftProtection;
    }

    public CheckingAccount(double balance) {
        super(balance);
    }

    public CheckingAccount(double balance, double overdraftProtection) {
        super(balance);
        this.overdraftProtection = overdraftProtection;
    }
    @Override
    public void withdraw(double amt) throws OverdraftException  {
        //当前余额足够弥补取款amt,则正常进行
        if (balance >= amt) {
            balance =balance-amt;
            return;
        }
        //存在没有透支保护的赤字
        if(this.overdraftProtection==0){

            throw new OverdraftException("Exception:no overdraft protection Deficit:",amt-this.balance);

        }
        //若当前余额+透支保障>amt
        if(balance+overdraftProtection>=amt){

            this.overdraftProtection=balance+this.overdraftProtection-amt;
            balance=0;
            return;
        }

        //若当前余额+透支保障 < amt
        if( this.balance+this.overdraftProtection < amt){

            throw new OverdraftException("Exception:Insufficient funds for overdraft protection Deficit:",amt);

        }

    }
}
